jQuery(document).ready(function() {

    // Override default error message
    jQuery.validator.messages.required = "";

    jQuery.validator.addMethod(
            "regex",
            function(value, element, regexp) {
                var check = false;
                return this.optional(element) || regexp.test(value);
            },
            "Please check your input."
            );


    // Override generation of error label
    jQuery("#updategallery_form").validate({
        rules: {
            galleryname: {
                required: true,
                /*regex: /^\s*[A-Za-z0-9 -_\s]+s*$/*/
                regex: /^\w+$/
            }
        },
        submitHandler: function(form) {

            if (jQuery('input[name="galleryname"]').val() == "") {
                alert('Enter gallery name');
                return false;
            }

            jQuery('#edit_gallery').attr('disabled', 'disabled');
            var form = jQuery("#updategallery_form");
            var form_data = form.serialize();

            jQuery('.ajax-loading').css('visibility', 'visible');

            jQuery.ajax({
                url: form.attr('action'),
                dataType: 'json',
                type: "POST",
                data: form_data,
                success: function(data, textStatus, xhr) {
                    jQuery('.ajax-loading').css('visibility', 'hidden');

                    if (data.action) {
                        jQuery('.loader').html(data.msg);
                        // REDIRECT
                        function promo_show() {
                            window.location.href = base_url + page + '/view_all?post_type=' + post_type + '&type=all';
                        }
                        window.setTimeout(function() {
                            promo_show();
                        }, 1001);
                    } else {
                        alert(data.msg);
                        jQuery('#edit_gallery').removeAttr('disabled');
                    }

                },
                error: function(xhr, textStatus, errorThrown) {
                    jQuery('.ajax-loading').css('visibility', 'hidden');
                    alert('Error ocured...\n Server not found.');
                    jQuery('#edit_gallery').removeAttr('disabled');

                }
            });

            return false;

        },
        errorPlacement: function(error, element) {
        }


    });

    jQuery('#edit_gallery').removeAttr('disabled');

    // Lightbox
    jQuery('a[rel=lightbox]').lightBox({
        overlayBgColor: '#000',
        overlayOpacity: 0.6,
        imageLoading: base_url + 'assets/img/lightbox/loading.gif',
        imageBtnClose: base_url + 'assets/img/lightbox/close.gif',
        imageBtnPrev: base_url + 'assets/img/lightbox/prev.gif',
        imageBtnNext: base_url + 'assets/img/lightbox/next.gif',
        imageBlank: base_url + 'assets/img/lightbox/blank.gif',
        containerResizeSpeed: 350,
        txtImage: 'Image',
        txtOf: 'of'
    });

});